package com.ysd.controller;

import com.alibaba.fastjson.JSON;
import com.ysd.dao.StudentMapper;
import com.ysd.entity.Student;
import com.ysd.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StudentController
 * @Description 学生信息控制器
 * @Author lvzhenzhen
 * @Date 2021/11/18 17:29
 */
@Controller
@RequestMapping("/stu")
public class StudentController {

    @Resource
    StudentMapper studentMapper;

    @RequestMapping(value = "/chaxun", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String chaxun(@RequestParam(value = "name", required = false) String stuname,
                         @RequestParam(value = "classid", required = false) Integer classid,
                         @RequestParam("page") int page,
                         @RequestParam("limit") int limit) {
        Map<String, Object> pars = new HashMap<>();
        pars.put("stuname", stuname);
        pars.put("classid", classid);
        pars.put("offset", (page-1)*limit);
        pars.put("limit", limit);

        List<Student> list = studentMapper.selectAll(pars);
        int count = studentMapper.count(pars);
        Result result = new Result(0, "查询成功", count, list);
        return JSON.toJSONString(result);

    }

    @RequestMapping(value = "/add", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String add(Student student) {

        int n = studentMapper.add(student);
        if (n == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }

    }

    @RequestMapping(value = "/update", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update(Student student) {

        int n = studentMapper.update(student);
        if (n == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }
}
